package com.gary.shiro;

import cn.hutool.http.HttpUtil;
import io.netty.util.NetUtil;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.UnknownHostException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SmsTest {

    public static void main(String[] args) throws Exception {

        String loginName = "";
        String userPassword = "";
        String mobile = "";
        String content = "";
        String Url = "http://192.168.64.52:8288/sms_service/services/smsService";
        String requestBody = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://service.core.weikang.com\"> "
                + "<soapenv:Header/>"
                + "<soapenv:Body>"
                + "<ser:service>"
                + "<ser:in0>" + loginName + "</ser:in0>"
                + "<ser:in1>" + userPassword + "</ser:in1>"
                + "<ser:in2>" + getMACAddress(InetAddress.getLocalHost()) + "</ser:in2>"
                + "<ser:in3></ser:in3>"
                + "<ser:in4>" + mobile + "</ser:in4>"
                + "<ser:in5>" + content + "</ser:in5>"
                + "</ser:service>"
                + "</soapenv:Body>"
                + "</soapenv:Envelope>";

        String postRet = HttpUtil.createPost(Url)
                .header("SOAPAction", "")
                .body(requestBody)
                .execute()
                .body();
        String regex = "<out>(.*)</out>";
        Pattern p = Pattern.compile(regex);
        Matcher match = p.matcher(postRet);
        String code = "";
        if (match.find()) {
            code = match.group(1);
        } else {
//            throw BizException.builder().msg("请求短信异常");
        }
    }

    /**
     * @Title: getMACAddress
     * @Description: 通过InetAddress对象获取MAC地址
     * @param inetAddress
     * @return
     * @throws Exception String
     * @author: wangyk
     * @date: 2020年11月23日 上午10:24:42
     * @version: 2.0.1
     */
    private static String getMACAddress(InetAddress inetAddress) throws Exception {
        // 获得网络接口对象（即网卡），并得到mac地址，mac地址存在于一个byte数组中。
        byte[] mac = NetworkInterface.getByInetAddress(inetAddress).getHardwareAddress();
        // 下面代码是把mac地址拼装成String
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < mac.length; i++) {
            if (i != 0) {
                sb.append("-");
            }
            // mac[i] & 0xFF 是为了把byte转化为正整数
            String s = Integer.toHexString(mac[i] & 0xFF);
            sb.append(s.length() == 1 ? 0 + s : s);
        }
        // 把字符串所有小写字母改为大写成为正规的mac地址并返回
        return sb.toString().toUpperCase();
    }
}